package com.dragon.backstage.controller;

import com.dragon.backstage.domain.param.menu.MenuAddParam;
import com.dragon.backstage.domain.param.menu.MenuChangeStatusParam;
import com.dragon.backstage.domain.param.menu.MenuChangeVisibleParam;
import com.dragon.backstage.domain.param.menu.MenuEditParam;
import com.dragon.backstage.domain.param.menu.MenuParam;
import com.dragon.backstage.domain.vo.menu.MenuAsyncPermissionVo;
import com.dragon.backstage.domain.vo.menu.MenuAsyncRouteVo;
import com.dragon.backstage.domain.vo.menu.MenuDetailVo;
import com.dragon.backstage.domain.vo.menu.MenuVo;
import com.dragon.backstage.service.MenuService;
import com.dragon.common.annotation.AuthPermission;
import com.dragon.common.annotation.UserOperatorLog;
import com.dragon.common.enums.BusinessType;
import com.dragon.common.utils.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2024-08-01 18:05:09
 */
@RestController
@RequestMapping("/api/system/menu")
@Tag(name = "菜单模块接口")
public class MenuController {

    @Resource
    private MenuService menuService;


    /**
     * 菜单列表
     *
     * @param menuParam 菜单列表请求参数
     * @return 菜单列表
     */
    @GetMapping("/list")
    @Operation(summary = "菜单列表")
    @AuthPermission(value = "system.menu.list")
    public AjaxResult<List<MenuVo>> list(MenuParam menuParam) {
        return menuService.menuList(menuParam);
    }

    /**
     * 前端菜单路由列表
     *
     * @return 前端菜单路由列表
     */
    @GetMapping("/asyncRoutes")
    @Operation(summary = "前端菜单路由")
    public AjaxResult<List<MenuAsyncRouteVo>> asyncRoutes() {
        return menuService.menuAsyncRoutes();
    }

    /**
     * 前端用户权限列表
     *
     * @return 用户权限列表
     */
    @GetMapping("/asyncPermissions")
    @Operation(summary = "前端用户权限")
    public AjaxResult<List<MenuAsyncPermissionVo>> asyncPermissions() {
        return menuService.menuAsyncPermissions();
    }

    /**
     * 菜单详情
     *
     * @param menuId 菜单ID
     * @return 菜单详情
     */
    @GetMapping("/detail")
    @Operation(summary = "菜单详情")
    @AuthPermission(value = "system.menu.detail")
    public AjaxResult<MenuDetailVo> detail(@RequestParam(name = "menuId", required = false) Long menuId) {
        return menuService.menuDetail(menuId);
    }

    /**
     * 添加菜单
     *
     * @param param 添加菜单请求参数
     * @return 添加结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加菜单")
    @AuthPermission(value = "system.menu.add")
    @UserOperatorLog(title = "菜单管理", businessType = BusinessType.INSERT)
    public AjaxResult<Void> add(@RequestBody MenuAddParam param) {
        return menuService.addMenu(param);
    }

    /**
     * 编辑菜单
     *
     * @param param 编辑菜单参数
     * @return 编辑结果
     */
    @PutMapping("/edit")
    @Operation(summary = "编辑菜单")
    @AuthPermission(value = "system.menu.edit")
    @UserOperatorLog(title = "菜单管理", businessType = BusinessType.UPDATE)
    public AjaxResult<Void> edit(@RequestBody MenuEditParam param) {
        return menuService.editMenu(param);
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除菜单")
    @AuthPermission(value = "system.menu.delete")
    @UserOperatorLog(title = "菜单管理", businessType = BusinessType.DELETE)
    public AjaxResult<Void> delete(@RequestParam(value = "menuId", required = false) Long menuId) {
        return menuService.deleteMenu(menuId);
    }

    /**
     * 更改显示状态
     *
     * @param param 更改显示状态请求参数
     * @return 更改结果
     */
    @PutMapping("/changeVisible")
    @Operation(summary = "更改菜单显示状态")
    @AuthPermission(value = "system.menu.change.visible")
    @UserOperatorLog(title = "菜单管理", businessType = BusinessType.MENU_CHANGE_VISIBLE)
    public AjaxResult<Void> changeVisible(@RequestBody MenuChangeVisibleParam param) {
        return menuService.changeVisible(param);
    }

    /**
     * 更改菜单状态
     *
     * @param param 更改菜单状态请求参数
     * @return 更改结果
     */
    @PutMapping("/changeStatus")
    @Operation(summary = "更改菜单状态")
    @AuthPermission(value = "system.menu.change.status")
    @UserOperatorLog(title = "菜单管理", businessType = BusinessType.MENU_CHANGE_STATUS)
    public AjaxResult<Void> changeStatus(@RequestBody MenuChangeStatusParam param) {
        return menuService.changeStatus(param);
    }

}
